package cn.exercise.controller;

import cn.exercise.pojo.Dog;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author :Colin
 * @version 创建时间: 2021/7/14 19:38
 */
@RestController
@RequestMapping("dog")
@CrossOrigin
public class DogController {
    @RequestMapping("get")
    public void get(){
        Dog dog = new Dog();
        dog.setAge(100);
        dog.setName("Punk");
        System.out.println(dog);
    }
    @RequestMapping("add")
    public String add(Integer age, String name){
        System.out.println(age+name);
        return age+name;//只能全部返回，不能只单独返回一个
    }
    @RequestMapping("add1")
    public Dog add1(Dog dog){
        System.out.println(dog);
        return dog;//既可以返回所有的参数，也可以只返回一个,console.log(data.age);
    }
    @RequestMapping("add2/{x}/{y}")
    public void add2(@PathVariable Integer x,@PathVariable String y){
        Dog dog = new Dog(x,y);
        System.out.println(dog);
    }
    @RequestMapping("add3")
    public Dog add3(Dog dog) throws Exception {
        System.out.println(dog);
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql:///colinbase?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root");
        String sql="insert into dog values(?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,dog.getName());
        ps.setInt(2,dog.getAge());
        ps.setString(3,dog.getGender());
        ps.setDouble(4,dog.getPrice());
        ps.executeUpdate();
        ps.close();
        conn.close();
        System.out.println("success");
        return dog;
    }
}
